import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { MonsterPlayer } from "../scene/player/entity/MonsterPlayer";
import { GameProxy } from "../mvc/GameProxy";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SGetBossQuestion } from "../../../Message/Public/SGetBossQuestion";
import { EntityUtil } from "../scene/layer/utils/EntityUtil";
import { EQuestionType } from "../../../Message/Public/EQuestionType";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
import { GameConst } from "../../component/gconst/GameConst";
import { SAnswerBossQuestionRes } from "../../../Message/Public/SAnswerBossQuestionRes";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { IView } from "../../mvc/interfaces/IView";
import { QuestionBossModule } from "../../../modules/QuestionBossModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { IQuestionBossModule } from "../../../modules/interfaces/IQuestionBossModule";
type int = number;
//class QuestionBossController
    
    export  class QuestionBossController extends Controller
    {
       

        /* internal  */doGetBossQuestion(arg1: DataEvent): void
        {
            this._selectedQuestionBoss = arg1.data as MonsterPlayer;
            var loc1=this._selectedQuestionBoss.entityInfo.entityInfo.entityId;
            GameProxy.copy.getBossQuestion(loc1);
            return;
        }

        /* internal  */doAnswerBossQuestion(arg1: DataEvent): void
        {
            var loc1=arg1.data["entityId"] as SEntityId;
            var loc2=arg1.data["answer"] as string;
            GameProxy.copy.answerBossQuestion(loc1, loc2);
            return;
        }

        public onGetGuildBossQuestion(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SGetBossQuestion;
            if (this._selectedQuestionBoss && EntityUtil.toString(loc1.entityId) == this._selectedQuestionBoss.entityID) 
            {
                if (this.questionType != EQuestionType._EQuestionTypeGuildStruggle) 
                {
                    if (this.questionType == EQuestionType._EQuestionTypeArenaActive) 
                    {
                        this.questionModule.showBossQuestionModule(loc1);
                    }
                }
                else 
                {
                    this.questionModule.showBossQuestionDialog(loc1);
                    this.questionModule.updateQuestionDialogTitle(this._selectedQuestionBoss.bossInfo.boss.name);
                }
                RolePlayer.instance.addEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
                Dispatcher.addEventListener(EventName.Convey_Res, this.checkOutDistance);
                Dispatcher.addEventListener(EventName.ChangeScene, this.checkOutDistance);
            }
            return;
        }

        /* internal  */checkOutDistance(arg1: Object /* flash.events.Event */): void
        {
            if (this._selectedQuestionBoss && ThingUtil.entityUtil.getDistanceByTile(this._selectedQuestionBoss) <= GameConst.NpcTaskDistance) 
            {
                this.questionModule.hideBossQuestionDialog(-1);
                RolePlayer.instance.removeEventListener(PlayerEvent.GIRD_WALK_END, this.checkOutDistance);
                Dispatcher.removeEventListener(EventName.Convey_Res, this.checkOutDistance);
                Dispatcher.removeEventListener(EventName.ChangeScene, this.checkOutDistance);
            }
            return;
        }

        /* internal  */doAnswerBossQuestionRes(arg1: DataEvent): void
        {
            this.questionModule.hideBossQuestionDialog(EQuestionType._EQuestionTypeGuildStruggle);
            return;
        }

        /* internal  */onAnswerBossQuestionRes(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SAnswerBossQuestionRes;
            if (this._selectedQuestionBoss && EntityUtil.toString(loc1.entityId) == this._selectedQuestionBoss.entityID) 
            {
                if (this.questionType != EQuestionType._EQuestionTypeGuildStruggle) 
                {
                    if (this.questionType == EQuestionType._EQuestionTypeArenaActive) 
                    {
                        this.questionModule.showAnswerQuestionResult(loc1);
                    }
                }
                else if (loc1.isRight) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60419));
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(60420));
                }
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._questionModule) 
            {
                this._questionModule = new QuestionBossModule();
            }
            return this._questionModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.GuildCopyGetBossQuestion, this.doGetBossQuestion);
            Dispatcher.addEventListener(EventName.GuildCopyAnswerBossQuestion, this.doAnswerBossQuestion);
            Dispatcher.addEventListener(EventName.GuildCopyAnswerBossQuestionRes, this.doAnswerBossQuestionRes);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGetBossQuestion, this.onGetGuildBossQuestion);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicAnswerBossQuestionRes, this.onAnswerBossQuestionRes);
            return;
        }

        public get questionModule(): IQuestionBossModule
        {
            if (!this._questionModule) 
            {
                this.initView();
            }
            return this._questionModule;
        }

        public get questionType(): int
        {
            if (this._selectedQuestionBoss) 
            {
                return this._selectedQuestionBoss.bossInfo.boss.maxLife;
            }
            return -1;
        }

        private /* var */_questionModule: IQuestionBossModule;

        private /* var */_selectedQuestionBoss: MonsterPlayer;
    }
